package com.eventosgasteiz.server;

import java.io.IOException;
import java.util.List;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.eventosgasteiz.server.bbdd.Event;
import com.eventosgasteiz.server.bbdd.PMF;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class BorrarEventoServlet extends HttpServlet {
    /**
	 * 
	 */
	private static final long serialVersionUID = 1477736008820465940L;
	private static final Logger log = Logger.getLogger(BorrarEventoServlet.class.getName());
	
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
                throws IOException {
    	

		String id = req.getParameter("id");
		String optionStr = req.getParameter("option");
		Character option = optionStr.charAt(0);
		PersistenceManager pmE = PMF.get().getPersistenceManager();
		String query = "select from " + Event.class.getName() + " where id=="+id;
		List<Event> eventos = (List<Event>) pmE.newQuery(query).execute();
		Event e = eventos.get(0);
		pmE.close();
	    	
        e.setOption(option);
        
        PersistenceManager pm = PMF.get().getPersistenceManager();
        try {
            pm.makePersistent(e);
        } finally {
            pm.close();
        }
        log.info("Evento actualizado");
        Gson json = new GsonBuilder().disableHtmlEscaping().create();
        log.info("JSON: "+json.toJson(e));
        resp.sendRedirect("/ListarEventos.jsp");
		
    }
}
